package jmine.tec.sync.channel;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import jmine.tec.sync.DataPushChannel;
import jmine.tec.utils.collection.MultiMap;
import jmine.tec.utils.collection.impl.EmptyMultiMap;
import jmine.tec.utils.debug.impl.DebugRuntimeFactory;
import jmine.tec.utils.io.http.Connector;
import jmine.tec.utils.io.http.HttpMethod;
import jmine.tec.utils.io.http.exception.HttpException;

/**
 * {@link DataPushChannel} que posta para um servidor HTTP via post
 * 
 * @author takeshi
 */
public class HttpDataPushChannel extends AbstractDataPushChannel implements DataPushChannel {

    private Connector connector;

    private String path = "";

    private final MultiMap<String, String> instance = EmptyMultiMap.<String, String> getInstance();

    /**
     * {@inheritDoc}
     */
    public void pushCommandsAsBytes(byte[] data) throws IOException {
        try {
            DebugRuntimeFactory.getInstance().getOrCreateCounter("dataPushChannel").add("data transfered", data.length);
            this.connector.makeRequestWithBody(this.getPath(), HttpMethod.POST, this.instance, this.instance,
                    new ByteArrayInputStream(data));
        } catch (HttpException e) {
            throw new IOException(e.getMessage());
        }
    }

    /**
     * @return the connector
     */
    public Connector getConnector() {
        return this.connector;
    }

    /**
     * @param connector the connector to set
     */
    public void setConnector(Connector connector) {
        this.connector = connector;
    }

    /**
     * @return the path
     */
    public String getPath() {
        return this.path;
    }

    /**
     * @param path the path to set
     */
    public void setPath(String path) {
        this.path = path;
    }

}
